package com.example.administrator.test;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Slider3Activity extends AppCompatActivity {
    ViewPager viewPager;
    int[]mViewId = new int[]{R.layout.view1,R.layout.view2,R.layout.view3,R.layout.view4};
    List<View> view_list = new ArrayList<>();

    //声明防止底部小圆点得LinearLayout
    LinearLayout linearLayout;
    //底部小圆点的集合
    List<ImageView> mDots = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slider3);
        ActionBar bar = getSupportActionBar();
        if(bar!=null){
            bar.hide();
        }



        viewPager = (ViewPager)findViewById(R.id.viewPager3);
        //添加动画
        viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        //循环添加XML页面资源
        for(int i=0;i<mViewId.length;i++){
            //通过layout加载页面资源
            View view = getLayoutInflater().inflate(mViewId[i],null);
            view_list.add(view);
        }
        //绑定Adapter适配器
        viewPager.setAdapter(new My3Adapter());
        //绑定PageChangeListener监听器
        viewPager.addOnPageChangeListener(new MyOnPageChangeListener());

        linearLayout = (LinearLayout)findViewById(R.id.linerLayout);
        //初始化小圆点集合
        for (int i=0;i<mViewId.length;i++){
            ImageView imageView = new ImageView(this);
            //设置小圆点得宽度高
            imageView.setLayoutParams(new ViewGroup.LayoutParams(40,40));
            //小圆点得边距
            imageView.setPadding(5,5,5,5);
            //小圆点的图片
            imageView.setImageResource(R.mipmap.circle1);
            mDots.add(imageView);
            //添加到LinearLayout
            linearLayout.addView(imageView);
        }
        //初始化小圆点
        mDots.get(0).setImageResource(R.mipmap.circle2);

    }

    class My3Adapter extends PagerAdapter{
            @Override
            public int getCount() {
                //返回XML资源长度
                return mViewId.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object o) {
            //判断资源是否加载正确
                return view==o;
            }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //移除资源
            View view = view_list.get(position);
            container.removeView(view);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //初始化资源
            View view = view_list.get(position);
            container.addView(view);
            return view;
        }
    }


    //绑定OnPageChangeListener
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            //循环将所有的小圆点全部设置黑色
            for(int index=0;index<mViewId.length;index++){
                mDots.get(index).setImageResource(R.mipmap.circle1);
            }
            //将被选中图片的小圆点设置为绿色
            mDots.get(i).setImageResource(R.mipmap.circle2);
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }


    public void start_button(View view)
    {
        Intent intent = new Intent(Slider3Activity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

}
